<template>
  <div class="chart-card" style="grid-column: span 4;">
    <h2><i class="icon-balance-scale"></i> 亚健康与正常体质对比</h2>
    <div ref="healthCompareChart" class="chart-container"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts';

export default {
  name: 'HealthCompareChart',
  mounted() {
    this.initChart();
    window.addEventListener('resize', this.handleResize);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.handleResize);
  },
  methods: {
    initChart() {
      const chart = echarts.init(this.$refs.healthCompareChart);
      const option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        legend: {
          data: ['正常体质', '亚健康体质'],
          bottom: 10
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '15%',
          containLabel: true
        },
        xAxis: {
          type: 'value',
          boundaryGap: [0, 0.01]
        },
        yAxis: {
          type: 'category',
          data: ['精力状态', '睡眠质量', '食欲消化', '情绪稳定', '免疫力', '适应能力']
        },
        series: [
          {
            name: '正常体质',
            type: 'bar',
            data: [90, 85, 88, 82, 87, 84],
            itemStyle: {
              color: '#4CAF50'
            },
            label: {
              show: true,
              position: 'right'
            }
          },
          {
            name: '亚健康体质',
            type: 'bar',
            data: [40, 35, 45, 30, 38, 42],
            itemStyle: {
              color: '#F44336'
            },
            label: {
              show: true,
              position: 'right'
            }
          }
        ]
      };
      chart.setOption(option);
    },
    handleResize() {
      this.$nextTick(() => {
        echarts.getInstanceByDom(this.$refs.healthCompareChart)?.resize();
      });
    }
  }
};
</script>

<style scoped>
.chart-container {
  height: 280px;
}
</style>